package edu.pku.bean
{
	import edu.pku.component.Node;
	import edu.pku.component.Link;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	public class Dependency
	{
	 	public static const  TYPE_UNKNOWN:int = -1;
		public static const  TYPE_PRESSURE_SENSOR:int= 1;
		public static const  TYPE_SOUND_SENSOR:int= 2;
		public static const  TYPE_FOG_SENSOR:int= 3;
		public static const  TYPE_INFRARED_SENSOR:int = 4;
		public static const  TYPE_VEDIO_SENSOR:int= 5;
		public static const  TYPE_RFID_SENSOR:int= 10;
		public static const  TYPE_METAL_SENSOR:int= 9;
		public static const  TYPE_VIBRATE_SENSOR:int=14;
		
		public static const  TYPE_TRACK:int= 11;
		public static const  TYPE_IDENTIFY:int=13;
		
		//Defines the dependency relation between basic service and composed service
		private var dependencies:Dictionary=new Dictionary();
		
		public function Dependency(){
			dependencies[TYPE_PRESSURE_SENSOR]=TYPE_TRACK;
			dependencies[TYPE_METAL_SENSOR]=TYPE_TRACK;
			dependencies[TYPE_VIBRATE_SENSOR]=TYPE_TRACK;
			dependencies[TYPE_SOUND_SENSOR]=TYPE_IDENTIFY;
		}
		
		private function belongsTo(fromNode:Node,toNode:Node):Boolean{
			if(dependencies[fromNode.nodeId]!=null&&dependencies[fromNode.nodeId]==toNode.nodeId)
				return true;
			else
				return false;
		}
		
		public function constructAllLinks(fromNodes:ArrayCollection,toNodes:ArrayCollection):ArrayCollection{
			var links:ArrayCollection=new ArrayCollection();
			for each(var fromNode:Node in fromNodes){
				for each(var toNode:Node in toNodes){
					if(belongsTo(fromNode,toNode))
					{		
						var newLink:Link=new Link();
						newLink.fromNode=fromNode;
			    		newLink.toNode=toNode;
			    		links.addItem(newLink);
					}
				}
			}
			return links;
		}
		
	}
}